
package epl

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/epl"
    eplReq "github.com/flipped-aurora/gin-vue-admin/server/model/epl/request"
)

type ErotableService struct {}
// CreateErotable 创建Erotable表记录
// Author [yourname](https://github.com/yourname)
func (erotableService *ErotableService) CreateErotable(ctx context.Context, erotable *epl.Erotable) (err error) {
	err = global.GVA_DB.Create(erotable).Error
	return err
}

// DeleteErotable 删除Erotable表记录
// Author [yourname](https://github.com/yourname)
func (erotableService *ErotableService)DeleteErotable(ctx context.Context, ID string) (err error) {
	err = global.GVA_DB.Delete(&epl.Erotable{},"id = ?",ID).Error
	return err
}

// DeleteErotableByIds 批量删除Erotable表记录
// Author [yourname](https://github.com/yourname)
func (erotableService *ErotableService)DeleteErotableByIds(ctx context.Context, IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]epl.Erotable{},"id in ?",IDs).Error
	return err
}

// UpdateErotable 更新Erotable表记录
// Author [yourname](https://github.com/yourname)
func (erotableService *ErotableService)UpdateErotable(ctx context.Context, erotable epl.Erotable) (err error) {
	err = global.GVA_DB.Model(&epl.Erotable{}).Where("id = ?",erotable.ID).Updates(&erotable).Error
	return err
}

// GetErotable 根据ID获取Erotable表记录
// Author [yourname](https://github.com/yourname)
func (erotableService *ErotableService)GetErotable(ctx context.Context, ID string) (erotable epl.Erotable, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&erotable).Error
	return
}
// GetErotableInfoList 分页获取Erotable表记录
// Author [yourname](https://github.com/yourname)
func (erotableService *ErotableService)GetErotableInfoList(ctx context.Context, info eplReq.ErotableSearch) (list []epl.Erotable, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&epl.Erotable{})
    var erotables []epl.Erotable
    // 如果有条件搜索 下方会自动创建搜索语句
    if len(info.CreatedAtRange) == 2 {
     db = db.Where("created_at BETWEEN ? AND ?", info.CreatedAtRange[0], info.CreatedAtRange[1])
    }
    
    if info.EroPartModel != nil && *info.EroPartModel != "" {
        db = db.Where("ero_part_model LIKE ?", "%"+ *info.EroPartModel+"%")
    }
    if info.EroPartId != nil && *info.EroPartId != "" {
        db = db.Where("ero_part_id LIKE ?", "%"+ *info.EroPartId+"%")
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }

	err = db.Find(&erotables).Error
	return  erotables, total, err
}
func (erotableService *ErotableService)GetErotablePublic(ctx context.Context) {
    // 此方法为获取数据源定义的数据
    // 请自行实现
}
